﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.IO;

namespace DownloadQuotidiano
{
    public partial class Form1 : Form
    {
        class WebClientWithCookies : WebClient
        {
            private CookieContainer _container = new CookieContainer();

            public CookieContainer Cookies
            {
                get { return _container; }
            }

            protected override WebRequest GetWebRequest(Uri address)
            {
                HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;

                request.UserAgent = "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; it-it) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8J3";
                if (request != null)
                {
                    //request.Method = "Post";
                    request.CookieContainer = _container;
                }

                return request;
            }
        }

        public Form1()
        {
            InitializeComponent();
            this.monthCalendar1.MaxDate = DateTime.Now;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tipoQuotidianoCmb.Text != "")
            {

                if (tipoQuotidianoCmb.Text.ToLower() == "il sole 24 ore")
                {
                    progressBar1.Value = 0;
                    WebClientWithCookies webclient = new WebClientWithCookies();

                    MergePdf merge = new MergePdf();
                    merge.SourceFolder = Directory.GetCurrentDirectory();
                    merge.DestinationFile = Path.Combine(Directory.GetCurrentDirectory(), "CdS" + this.monthCalendar1.SelectionStart.ToString(" - ddMMyyyy") + ".pdf");

                    webclient.Cookies.Add(new Cookie("beRCSapps", "RCSapps", "/", ".corriere.it"));
                                        
                    int page = 0;
                    progressBar1.Maximum = 1;
                        do{
                            string filename = page.ToString("000")+".pdf";
                            try
                            {
                                webclient.DownloadFile("http://ilsole24ore2.pagestreamer.com/deploy/" + this.monthCalendar1.SelectionStart.ToString("yyyyMMdd") + "/SOLE_SOLE/" + filename, filename);
                            }
                            catch {                                
                                break;
                            }
                            page++;
                            merge.AddFile(filename);
                            progressBar1.Maximum++;
                            progressBar1.Increment(1);
                        }while(true);                    

                    merge.Execute(true);
                    progressBar1.Increment(1);
                }
                else if (tipoQuotidianoCmb.Text.ToLower() == "corriere della sera")
                {
                    progressBar1.Value = 0;
                    WebClientWithCookies webclient = new WebClientWithCookies();

                    MergePdf merge = new MergePdf();
                    merge.SourceFolder = Directory.GetCurrentDirectory();
                    merge.DestinationFile = Path.Combine(Directory.GetCurrentDirectory(), "CdS" + this.monthCalendar1.SelectionStart.ToString(" - ddMMyyyy") + ".pdf");

                    webclient.Cookies.Add(new Cookie("beRCSapps", "RCSapps", "/", ".corriere.it"));

                    var txt = webclient.DownloadString("http://media2.corriere.it/mysynd/data/be/pdf2/CORRIEREFC_NAZIONALE_WEB/index.xml");

                    XDocument docIndex = XDocument.Parse(txt);

                    var pippo = (from issue in docIndex.Descendants("issue")
                                where issue.Attribute("name").Value == this.monthCalendar1.SelectionStart.ToString("yyyyMMdd")
                                select issue).SingleOrDefault();

                    if (pippo != null)
                    {                        
                        progressBar1.Maximum = pippo.Descendants("document").Count() + 1;
                        foreach (var documentPdf in pippo.Descendants("document"))
                        {
                            string filename = documentPdf.Attribute("path").Value;
                            webclient.DownloadFile("http://media2.corriere.it/mysynd/data/be/pdf2/CORRIEREFC_NAZIONALE_WEB/" + this.monthCalendar1.SelectionStart.ToString("yyyyMMdd") + "/" + filename, filename);
                            merge.AddFile(filename);
                            progressBar1.Increment(1);
                        }
                    }

                    merge.Execute(true);
                    progressBar1.Increment(1);
                }
                else if (tipoQuotidianoCmb.Text.ToLower() == "gazzetta dello sport")
                {
                    progressBar1.Value = 0;
                    WebClientWithCookies webclient = new WebClientWithCookies();

                    MergePdf merge = new MergePdf();
                    merge.SourceFolder = Directory.GetCurrentDirectory();
                    merge.DestinationFile = Path.Combine(Directory.GetCurrentDirectory(), "gds" + this.monthCalendar1.SelectionStart.ToString(" - ddMMyyyy") + ".pdf");

                    webclient.Cookies.Add(new Cookie("beRCSapps", "RCSapps", "/", ".corriere.it"));

                    var txt = webclient.DownloadString("http://media2.corriere.it/mysynd/data/be/pdf2/GAZZETTAFC_NAZIONALE_WEB/index.xml");

                    XDocument docIndex = XDocument.Parse(txt);

                    var pippo = (from issue in docIndex.Descendants("issue")
                                 where issue.Attribute("name").Value == this.monthCalendar1.SelectionStart.ToString("yyyyMMdd")
                                 select issue).SingleOrDefault();

                    if (pippo != null)
                    {
                        progressBar1.Maximum = pippo.Descendants("document").Count() + 1;
                        foreach (var documentPdf in pippo.Descendants("document"))
                        {
                            string filename = documentPdf.Attribute("path").Value;
                            webclient.DownloadFile("http://media2.corriere.it/mysynd/data/be/pdf2/GAZZETTAFC_NAZIONALE_WEB/" + this.monthCalendar1.SelectionStart.ToString("yyyyMMdd") + "/" + filename, filename);
                            merge.AddFile(filename);
                            progressBar1.Increment(1);
                        }
                    }

                    merge.Execute(true);
                    progressBar1.Increment(1);
                }
            }
        }
    }
}
